/**
 * 定时执行任务
 * 随机启动
 **/
var Q = require('q');
var config = require('../config');
var Schema = require('jugglingdb').Schema;
var async = require('async');
var E = require('../error');
var schema = new Schema('mysql', config.db.api);
var M = require('./schema/simpleModel')(schema.adapter);
var schedule = require('node-schedule');
var _ = require('underscore');
var L = require('../logger.js');
var request = require('request');

/**
 * A任务，查询注册的 webevent 任务列表，进行调用
 */
var jobA = function(){
    //获取当前的时和分
    var now = new Date();
    var h = now.getHours();
    var m = now.getMinutes();
    //return;
    //var timeOfDay = h + ":" + m;

    M.find({table:'api_webevent',condition:"status = 1 and timeOfDay in ('*:*','*:" + m + "','"+h+":*','"+h+":"+m+"')",limit:100}).then(function(list){
        _.each(list,function(item){
            var url = item.url;
            request(url,function(err,response,body){
                L.info(body);
            });
        });
    }).catch(function(err){
        L.error(err);
    });
}

module.exports = {
    run:function(){
        var j1 = schedule.scheduleJob({ minute: 30}, jobA);
        var j2 = schedule.scheduleJob({ minute: 0}, jobA);
    }
}
